package com.ha8.netease.models
{
	import com.ha8.netease.App;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.PublicNotify;
	import com.ha8.netease.models.vo.CommentReqVO;
	import com.ha8.netease.models.vo.FavReqVO;
	import com.ha8.netease.models.vo.HomeReqVO;
	import com.ha8.netease.models.vo.MailReqVO;
	import com.ha8.netease.models.vo.MainReqVO;
	import com.ha8.netease.models.vo.MentionReqVO;
	import com.ha8.netease.models.vo.UserModel;
	import com.ha8.netease.models.vo.UsersReqVO;
	import com.ha8.netease.views.utils.DataUtil;
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.rogo.weibo.platform.netease.WeiboNetease;
	import com.rogo.weibo.platform.netease.api.account.DoAccount;
	import com.rogo.weibo.platform.netease.api.directmessage.DoDirectMessage;
	import com.rogo.weibo.platform.netease.api.favorite.DoFavorite;
	import com.rogo.weibo.platform.netease.api.friendship.DoFriendship;
	import com.rogo.weibo.platform.netease.api.oauth.DoOauth;
	import com.rogo.weibo.platform.netease.api.search.DoSearch;
	import com.rogo.weibo.platform.netease.api.status.DoStatus;
	import com.rogo.weibo.platform.netease.api.timeline.DoTimeline;
	import com.rogo.weibo.platform.netease.api.users.DoUsers;
	
	public class WeiboProxy extends Proxy
	{
		public function WeiboProxy()
		{
			super(NAME);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'weiboProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var weibo:WeiboNetease;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			weibo = new WeiboNetease(resultHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 登录
		 */		
		public function login(verify:String):void
		{
			weibo.getAccessToken(verify);
		}
		
		/**
		 * 获取授权码
		 */		
		public function oauth():void
		{
			weibo.getRequestToken(App.APP_KEY, App.APP_SECRET);
		}
		
		/**
		 * 获取首页微博
		 */		
		public function getHomeTimeline(vo:HomeReqVO):void
		{
			weibo.getHomeTimeline(vo.count, vo.since_id, vo.max_id, vo.trim_user);
		}
		
		/**
		 * 获取主页微博
		 */		
		public function getMainTimeline(vo:MainReqVO):void
		{
			weibo.getUserTimeline(vo.user_id, vo.screen_name, vo.name, vo.count, vo.since_id, vo.max_id, vo.trim_user);
		}
		
		/**
		 * 获取指定用户的收藏列表
		 */		
		public function getFavorite(vo:FavReqVO):void
		{
			weibo.getFavorites(vo.id, vo.count, vo.since_id);
		}
		
		/**
		 * 获取@我的微博
		 */		
		public function getMention(vo:MentionReqVO):void
		{
			weibo.getMentions(vo.count, vo.since_id, vo.max_id, vo.trim_user);
		}
		
		/**
		 * 获取用户关注列表
		 */		
		public function getFriends(vo:UsersReqVO):void
		{
			weibo.getFriends(vo.user_id, vo.screen_name, vo.cursor);
		}
		
		/**
		 * 添加关注 
		 */
		public function attention(user_id:String=null, screen_name:String=null):void{
			weibo.createFriendship(user_id, screen_name);
		}
		
		/**
		 * 获取收到的私信
		 */		
		public function getMail(vo:MailReqVO):void
		{
			weibo.getDirectMessage(vo.since_id, vo.count);
		}
		
		/**
		 * 获取发出的私信
		 */		
		public function getSendMail(vo:MailReqVO):void
		{
			weibo.getDirectMessageSend(vo.since_id, vo.count);
		}
		
		/**
		 * 获取粉丝列表
		 */		
		public function getFans(vo:UsersReqVO):void
		{
			weibo.getFollowers(vo.user_id, vo.screen_name, vo.cursor);
		}
		
		/**
		 * 获取评论我的列表
		 */		
		public function getComment(vo:CommentReqVO):void
		{
			weibo.getCommentsToMe(vo.count, vo.since_id, vo.max_id, vo.trim_user);
		}
		
		/**
		 * 获取我评论的列表
		 */		
		public function getCommentSend(vo:CommentReqVO):void
		{
			weibo.getCommentsByMe(vo.count, vo.since_id, vo.max_id, vo.trim_user);
		}
		
		/**
		 * 搜索用户
		 * */
		public function searchUser(q:String,page:String,per_page:String):void
		{
			weibo.searchUser(q);//,page,per_page
		}
		
		/**
		 * 搜索微博
		 * */
		public function searchWeibo(q:String,page:String,per_page:String):void
		{
			weibo.search(q);//,page,per_page
		}
		
		/**
		 * 搜索微博New
		 * */
		public function searchWeiboNew(q:String,page:String,per_page:String):void
		{
			weibo.searchStatus(q);//,page,per_page
		}
		
		/**
		 * 获取推荐的I达人的用户列表
		 * */
		public function getSuggestionsI(cursor:String=null, trim_user:Boolean=false):void{
			weibo.getSuggestionsI(cursor, trim_user);
		}
		
		/**
		 * 返回用户可能感兴趣的用户，随机返回指定数目的用户
		 * */
		public function getSuggestions(count:String=null, trim_user:Boolean=false):void{
			weibo.getSuggestions(count, trim_user);
		}
		
		/**
		 * 删除指定微博
		 * */
		public function destroyStatus(id:String):void{
			weibo.destroyStatus(id);
		}
		
		/**
		 * 收藏微博
		 * */
		public function addFavorite(id:String):void{
			weibo.addFavorite(id);
		}
		
			
		/**
		 * 发布一条新微博
		 * */
		public function postStatus(status:String, lat:String = null, long:String = null, vid:String = null):void
		{
			weibo.postStatus(status,lat,long,vid);
		}
			
		/**
		 * 评论一条新微博
		 * */
		public function replyStatus(id:String, status:String, is_retweet:String = null, is_comment_to_root:String = null):void
		{
			weibo.replyStatus(id,status,is_retweet,is_comment_to_root);
		}
			
		/**
		 * 转发一条新微博
		 * */
		public function retweetStatus(id:String, status:String = null, is_retweet:String = null, is_comment_to_root:String = null):void
		{
			weibo.retweetStatus(id,status,is_retweet,is_comment_to_root);
		}
		
		
		
		/**
		 * 请求成功回调函数
		 * 
		 * @param cmd
		 * @param data
		 */		
		private function resultHandler(cmd:String, data:Object):void
		{
			switch (cmd)
			{
				case DoOauth.CMD_REQUEST_TOKEN:
				{
					weibo.authRequestToken();
					break;
				}
				case DoOauth.CMD_ACCESS_TOKEN:
				{
					weibo.verifyCredentials();
					break;
				}
				case DoAccount.CMD_VERIFY_CREDENTIALS:
				{
					var me:UserModel = new UserModel(data);
					App.getInstance().me = me;
					sendNotification(PublicNotify.LOGIN_RESULT);
					break;
				}
				case DoTimeline.CMD_HOME_TIMELINE:
				{
					sendNotification(PrivateNotify.GET_HOMETIMELINE_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoTimeline.CMD_USER_TIMELINE:
				{
					sendNotification(PrivateNotify.GET_USERTIMELINE_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoSearch.CMD_SEARCH_USER:
				{
					sendNotification(PrivateNotify.GET_SEARCH_USER_RESULT, data);
					break;
				}
				case DoSearch.CMD_SEARCH:
				{
					sendNotification(PrivateNotify.GET_SEARCH_WEIBO_RESULT, data);
					break;	
				}
				case DoSearch.CMD_SEARCH_STATUS:
				{	
					sendNotification(PrivateNotify.GET_SEARCH_WEIBO_NEW_RESULT, data);
					break;
				}
				case DoFavorite.CMD_FAVORITES:
				{
					sendNotification(PrivateNotify.GET_USERFAVORITE_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoTimeline.CMD_MENTIONS:
				{
					sendNotification(PrivateNotify.GET_USERMENTION_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoFriendship.CMD_GET_FRIENDS:
				{
					sendNotification(PrivateNotify.GET_FRIENDS_RESULT, DataUtil.ObjToUsers(data.users));
					break;
				}
				case DoFriendship.CMD_GET_FOLLOWERS:
				{
					sendNotification(PrivateNotify.GET_FANS_RESULT, DataUtil.ObjToUsers(data.users));
					break;
				}
				case DoDirectMessage.CMD_DIRECTMESSAGE:
				{
					sendNotification(PrivateNotify.GET_MAIL_RESULT, DataUtil.objToMails(data));
					break;
				}
				case DoDirectMessage.CMD_DIRECTMESSAGE_SEND:
				{
					sendNotification(PrivateNotify.GET_MAILSEND_RESULT, DataUtil.objToMails(data));
					break;
				}
				case DoTimeline.CMD_GET_COMMENTS_TO_ME:
				{
					sendNotification(PrivateNotify.GET_COMMENT_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoTimeline.CMD_GET_COMMENTS_BY_ME:
				{
					sendNotification(PrivateNotify.GET_COMMENTSEND_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoStatus.CMD_STATUS_RETWEET:
				{
					sendNotification(PrivateNotify.GET_RETWEET_STATUS_RESULT, data);
					break;
				}	
				case DoStatus.CMD_STATUS_UPDATE:
				{
					sendNotification(PrivateNotify.GET_PUBLISH_RESULT, DataUtil.objToMicroblogs(data));
					break;
				}
				case DoStatus.CMD_STATUS_DESTROY:
				{
					sendNotification(PrivateNotify.GET_DESTROY_STATUS_RESULT, data);
					break;
				}
				case DoFavorite.CMD_ADD_FAVORITES:
				{
					sendNotification(PrivateNotify.GET_ADD_FAV_RESULT, data);
					break;
				}
				case DoUsers.CMD_USERS_SUGGESTIONS_I:
				{
					sendNotification(PrivateNotify.GET_SUGGESTIONS_I_RESULT, data);
					break;
				}
				case DoUsers.CMD_USERS_SUGGESTIONS:
				{
					sendNotification(PrivateNotify.GET_SUGGESTIONS_RESULT, data);
					break;
				}
				case DoStatus.CMD_STATUS_REPLY: 
				{
					sendNotification(PrivateNotify.COMMENT_RESULT, data);
					break;
				}
				case DoStatus.CMD_STATUS_RETWEET:
				{
					sendNotification(PrivateNotify.GET_REPOST_RESULT, data);
					break;
				}
				case DoFriendship.CMD_FRIENDSHIP_CREATE:
				{
				}
				default:
					break;
			}
		}
		
		/**
		 * 请求失败回调函数
		 * 
		 * @param cmd
		 * @param data
		 */		
		private function errorHandler(cmd:String, data:Object):void
		{
			switch (cmd)
			{
				case DoOauth.CMD_REQUEST_TOKEN:
					break;
				case DoOauth.CMD_ACCESS_TOKEN:
				case DoAccount.CMD_VERIFY_CREDENTIALS:
					sendNotification(PublicNotify.LOGIN_ERROR);
					break;
				case DoTimeline.CMD_HOME_TIMELINE:
					sendNotification(PrivateNotify.GET_HOMETIMELINE_ERROR);
					break;
				case DoTimeline.CMD_USER_TIMELINE:
					sendNotification(PrivateNotify.GET_USERTIMELINE_ERROR);
					break;
				case DoFavorite.CMD_FAVORITES:
					sendNotification(PrivateNotify.GET_USERFAVORITE_ERROR);
					break;
				case DoTimeline.CMD_MENTIONS:
					sendNotification(PrivateNotify.GET_USERTIMELINE_ERROR);
					break;
				case DoFriendship.CMD_GET_FRIENDS:
					break;
				case DoFriendship.CMD_GET_FRIENDS:
					sendNotification(PrivateNotify.GET_FRIENDS_ERROR);
					break;
				case DoFriendship.CMD_GET_FOLLOWERS:
					sendNotification(PrivateNotify.GET_FANS_ERROR);
					break;
				case DoDirectMessage.CMD_DIRECTMESSAGE:
					sendNotification(PrivateNotify.GET_MAIL_ERROR);
					break;
				case DoDirectMessage.CMD_DIRECTMESSAGE_SEND:
					sendNotification(PrivateNotify.GET_MAILSEND_ERROR);
					break;
				case DoTimeline.CMD_GET_COMMENTS_TO_ME:
					sendNotification(PrivateNotify.GET_COMMENT_ERROR);
					break;
				case DoTimeline.CMD_GET_COMMENTS_BY_ME:
					sendNotification(PrivateNotify.GET_COMMENTSEND_ERROR);
					break;
				case DoSearch.CMD_SEARCH_STATUS:
					break;
				case DoStatus.CMD_STATUS_UPDATE:
					sendNotification(PrivateNotify.GET_PUBLISH_ERROR);
					break;
				case DoStatus.CMD_STATUS_DESTROY:
					sendNotification(PrivateNotify.GET_DESTROY_STATUS_ERROR);
					break;
				case DoFavorite.CMD_ADD_FAVORITES:
					sendNotification(PrivateNotify.GET_ADD_FAV_ERROR);
					break;
				case DoUsers.CMD_USERS_SUGGESTIONS:
					sendNotification(PrivateNotify.GET_SUGGESTIONS_ERROR);
					break;
				case DoStatus.CMD_STATUS_REPLY:
					sendNotification(PrivateNotify.COMMENT_ERROR);
					break;
				case DoStatus.CMD_STATUS_RETWEET:
					sendNotification(PrivateNotify.GET_REPOST_ERROR);
					break;
			}
		}
		
	}
}